package com.jeect.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

@Component("demoAuthenticationSuccessHandler")
public class DemoAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException {
        System.out.println("SavedRequestAwareAuthenticationSuccessHandler.........onAuthenticationSuccess（）....");
//        if(LogonType.JSON.equals(securityProperties.getAuthentication().logonType)){
            response.setContentType("application/json;charset=UTF-8");
            String s = objectMapper.writeValueAsString(authentication);

        HttpSession session = request.getSession();
        session.setAttribute("name",authentication.getName());
        Enumeration<String> enumeration =  session.getAttributeNames();
        while (enumeration.hasMoreElements()){
            String key  = enumeration.nextElement();
            System.out.println(key+"==="+session.getAttribute(key));
        }

            System.out.println("s===="+s);
            response.getWriter().write(s);
//        }else{
//            super.onAuthenticationSuccess(request,response,authentication);
//        }
    }
}
